package com.blackline.UI.REPORTS;

import java.util.List;

import org.apache.log4j.Logger;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import com.blackline.auto.Report.RunStdReportsWithAllRoles;
/**
 * @author XBO
 * @version Jan, 2013
 * <p>From Navigation Menu, click REPORTS and then click Enhanced Reporting link.
 * Please refer to UI.Reporting.png in doc-files.
 */
public class Reporting {
	
	private static Logger logger = Logger.getLogger(Reporting.class);
	
	/**
	 * Error loading requested data message
	 */
	@FindBy (className = "webControls_BlackLineGrid_Error")
	public WebElement Error_Lading_Requested_Data_message;
	
	/**
	 * Name filter text field
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_Filter']/td/input[1]")
	public WebElement Name_text_field;
	
	/**
	 * Schedule Name filter text field under My Scheduled Report node.
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_Filter']/td/input[1]")
	public WebElement Schedule_Name_text_field;
	
	
	/**
	 * Description filter text field
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_Filter']/td[7]/input[1]")
	public WebElement Description_text_field;
	
	/**
	 * Status filter text field
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_Filter']/td[6]/input[1]")
	public WebElement Status_text_field;
	
	/**
	 * Start date filter text field
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_Filter']/td[7]/input[1]")
	public WebElement StartDate_text_field;
	
	/**
	 * End date filter text field
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_Filter']/td[8]/input[1]")
	public WebElement EndDate_text_field;
	
	/**
	 * Report filter text field under My Scheduled Report node.
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_Filter']/td[7]/input[1]")
	public WebElement Report_text_field;
	
	/**
	 * "There are no records." text when no item is filtered out.
	 */
	@FindBy (css = "tr.webControls_BlackLineGrid_NoRecords > td")
	public WebElement There_are_no_records_text;
	
	
	/**
	 * The "New Report" link
	 */
	@FindBy (xpath = "(//a[@class='slimtree-buttons-new-report'])")
	public WebElement new_Report_link;
	
	/**
	 * Add Folder link
	 */
	@FindBy(xpath="(//a[@class = 'slimtree-buttons-button slimtree-buttons-add'])")
	public WebElement Add_Folder_Link;
	
	/**
	 * Delete Folder link
	 */
	@FindBy(xpath="(//a[@class = 'slimtree-buttons-button slimtree-buttons-delete'])")
	public WebElement Delete_Folder_Link;
	
	/**
	 * Rename Folder link
	 */
	@FindBy(xpath="(//a[@class = 'slimtree-buttons-button slimtree-buttons-rename'])")
	public WebElement Rename_Folder_Link;
	

	/**
	 * TestFolder link
	 */
	@FindBy(xpath="//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']//span[contains(text(),'TestFolder')]")
	public WebElement TestFolder_Link;
	
	
	
	
	
	
	/**
	 * The "Report Log" node in the left side tree.
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']/ul/li[1]/div/span[2]")
	public WebElement Report_Log_nodeInLeftTree;
	
	/**
	 * The "My Favorite Reports" node in the left side tree.
	 */
	@FindBy (xpath = "//span[contains(text(),'My Favorite Reports')]")
	public WebElement My_Favorite_Reports_nodeInLeftTree;
	
	
	/*
	 * The "UPS Reports" node in the left side tree.
	 
	@FindBy (css = ".rtUL .rtLI:nth-child(2) .rtIn")
	public WebElement UPS_Reports_nodeInLeftTree;*/
	
	/**
	 * The "UPS Reports" node in the left side tree.
	 */
	@FindBy (xpath = "//span[contains(text(),'UPS Reports')]")
	public WebElement UPS_Reports_nodeInLeftTree;
	
	
	/**
	 * The + of  node UPS Reports node in the left side tree.
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']/ul/li[2]/div/span[2]")
	public WebElement AddIcon_UPSReports;
	
	
	/**
	 * The "UPS" node in the left side tree.
	 */
	@FindBy(xpath = "//ul/li[2]/ul/li/div/span[contains(text(),'UPS')]")
	public WebElement UPS_nodeInLeftTree;
	
	
	/**
	 * The "My Scheduled Reports" node in the left side tree.
	 */
	@FindBy (css = ".rtUL .rtLI:nth-child(4) .rtIn")
	public WebElement My_scheduled_Report_nodeInLeftTree;
	
	/**
	 * The "BlackLine Reports" node in the left side tree.
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']/ul/li[5]/div/span[3]")
	public WebElement BlackLine_Reports_nodeInLeftTree;
	
	
	/**
	 * The + of  node My Favorite Reports node in the left side tree.
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']/ul/li[3]/div/span[2]")
	public WebElement AddIcon_MyFavoriteReports;
	
	/**
	 * The + of  node BlackLine Reports node in the left side tree.
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']/ul/li[5]/div/span[2]")
	public WebElement AddIcon_BlackLineReports;
	
	/**
	 * The "Accounts" node in the left side tree.
	 */
	@FindBy (xpath = "//span[contains(text(),'Accounts')]")
	public WebElement Accounts_nodeInLeftTree;
	
	/**
	 * The + of  node Account under BlackLine Reports node in the left side tree.
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']/ul/li[5]/ul/li/div/span[2]")
	public WebElement AddIcon_Account;
	
	
	/**
	 * The "Analysis" node in the left side tree.
	 */
	@FindBy (xpath = "//span[contains(text(),'Analysis')]")
	public WebElement Analysis_nodeInLeftTree;
	
	/**
	 * The Information node under BlackLine Reports node in the left side tree.
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_ftPage_folderTree']/ul/li[5]/ul/li/ul/li[2]/div/span[2]")
	public WebElement Information_nodeInLeftTree;
	
	/**
	 * The "Status" node in the left side tree.
	 */
	@FindBy (xpath = "//span[contains(text(),'Status')]")
	public WebElement Status_nodeInLeftTree;
	
	
	/**
	 * The "BlackLine Report Template" node in the left side tree.
	 */
	@FindBy (xpath = "//span[contains(text(),'BlackLine Report Templates')]")
	public WebElement BlackLine_Report_Templates_nodeInLeftTree;
	
	
	
	
	
	
	/**
	 * The Run link of the searched result. (Suppose only have one result.)
	 */
	@FindBy (className = "report-run")
	public WebElement Run_linkOfSearchedResult;
	
	/**
	 * The Copy link of the searched result. (Suppose only have one result.)
	 */
	@FindBy (className = "report-copy")
	public WebElement Copy_linkOfSearchedResult;
	
	/**
	 * The Schedule link of the searched result. (Suppose only have one result.)
	 */
	@FindBy (className = "report-schedule")
	public WebElement Schedule_linkOfSearchedResult;
	
	/**
	 * The Edit link of the searched result. (Suppose only have one result.)
	 */
	@FindBy (className = "report-edit")
	public WebElement Edit_linkOfSearchedResult;
	
	/**
	 * The Delete link of the searched result. (Suppose only have one result.)
	 */
	@FindBy (className = "report-delete-log")
	public WebElement Delete_linkOfSearchedResult;
	
	/**
	 * The Daily link of the searched Scheduled Report. (Suppose only have one result.)
	 */
	@FindBy (className = "schedule-entry-edit")
	public WebElement Daily_linkOfSearchedScheduledReport;
	
	/**
	 * The Weekly link of the searched Scheduled Report. (Suppose only have one result.)
	 */
	@FindBy (className = "schedule-entry-edit")
	public WebElement Weekly_linkOfSearchedScheduledReport;
	
	/**
	 * The Monthly link of the searched Scheduled Report. (Suppose only have one result.)
	 */
	@FindBy (className = "schedule-entry-edit")
	public WebElement Monthly_linkOfSearchedScheduledReport;
	
	/**
	 * The Monthly link of the searched Scheduled Report. (Suppose only have one result.)
	 */
	@FindBy (className = "schedule-entry-delete")
	public WebElement Delete_linkOfSearchedScheduledReport;
	
	
	/**
	 * The View link of the 1st searched result.
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_RowOdd']/td[1]/a")
	public WebElement View_linkOf1stSearchedResult;
	
	/**
	 * The Status string of the 1st searched result.
	 */
	@FindBy (xpath = "//tr[@class='webControls_BlackLineGrid_RowOdd']/td[6]/span")
	public WebElement Status_of1stSearchedResult;
	
	
	/**
	 * The Status string of the "Schedule Name" searched result.
	 */
	@FindBy (css = ".webControls_BlackLineGrid_RowOdd:nth-child(1) td:nth-child(2) ")
	public WebElement Schedule_Name_SearchedResult;
	
	
	
	/**
	 * Refresh button
	 */
	@FindBy (xpath = "//button[@class='webControls_ToolBar_Btn webControls_ToolBar_Btn_Refresh']")
	public WebElement Refresh_button;
	
	/**
	 * log body
	 */
	@FindBy (xpath = "//div[@id='gridContainer']/div[1]/table/tbody")
	public WebElement Log_Body;
	/**
	 * The string of the "Method" in searched result.
	 */
	@FindBy (css = ".webControls_BlackLineGrid_RowOdd:nth-child(1) td:nth-child(5) ")
	public WebElement Method_SearchedResult;
	/**
	 * The string of the "Report" in searched result.
	 */
	@FindBy (css = ".webControls_BlackLineGrid_RowOdd:nth-child(n) td:nth-child(4) ")
	public List<WebElement> Report_LogOdd;
	/**
	 * The string of the "Report" in searched result.
	 */
	@FindBy (css = ".webControls_BlackLineGrid_RowEven:nth-child(n) td:nth-child(4) ")
	public List<WebElement> Report_LogEven;
	
	//-----------------------------------------------------------------------------//
	/*
	 * The following is the pop-up "Copy Report" dialog box while copying a report
	 */
	
	
	/**
	 * Report Name text field
	 */
	@FindBy (id = "new-report-name")
	public WebElement report_Name_forCopiedReport_textField;
	
	/**
	 * Destination Folder drop-down list
	 */
	@FindBy (id = "ctl00_ctl00_contentBody_cphMain_editReportPrompt_copyToFolderComboTree_ctl00_Input")
	public WebElement Destination_Folder_forCopiedReport_dropDownList;
	
	/**
	 * My Favoriate Reports node in the Destination Folder drop-down list
	 */
	@FindBy (xpath = "//div[@id='ctl00_ctl00_contentBody_cphMain_editReportPrompt_copyToFolderComboTree_ctl00_i0_copyToFolderComboTree_tree']//span[contains(text(),'My Favorite Reports')]")
	public WebElement my_Favoriate_Reports_inDestinationFolder_dropDownListElement;
	
	
	/*
	 * Number of result for particular filter 
	 */
	@FindBy (xpath="//li[@class='webControls_BlackLineGrid_Paging_Results']/div[1]")
	public WebElement Number_of_results;
	
	/**
	 * Ok button in both "Copy Report" dialog and the Alert dialog after finished running a report.
	 */
	@FindBy (id = "aOk")
	public WebElement OK_button;
	
	/**
	 * Cancel button
	 */
	@FindBy (id = "aCancel")
	public WebElement Cancel_button_inReportCopyingDialog;
	

	//--------------------------------------------------------------------//
	/*
	 * The following is the pop-up dialog box after clicking Run button for a report
	 */
	
	/**
	 * Run Report button in the  pop-up dialog box after clicking Run button for a report
	 */
//	@FindBy (css = "webcontrols_toolbar_icon_save.core_clearfix")
	@FindBy (xpath = "//div[@class = 'webcontrols_toolbar_icon_save core_clearfix']")
	public WebElement Run_Report_button;
	
	/**
	 * Cancel button in the  pop-up dialog box after clicking Run button for a report
	 */
	@FindBy (xpath = "//div[@class = 'webcontrols_toolbar_icon_cancel core_clearfix']")
	public WebElement Cancel_button;
	
	/**
	 * Run Report button in the  pop-up dialog box after clicking Run button for a report
	 */
	@FindBy (id = "tbibFiltersClear")
	public WebElement Clear_button;
	//--------------------------------------------------------------------//
	
	
	/**
	 * Verify the just-run reports succeeded or failed under Report Log node.
	 */
	public void VerifyReportResultsUnderReportLog(String[] reportNames) throws Exception
	{
		this.Report_Log_nodeInLeftTree.click();
		Thread.sleep(3000);
		
		for (int i=0; i<reportNames.length; i++)
		{
			this.Name_text_field.clear();
			this.Name_text_field.sendKeys(reportNames[i]);
			this.Name_text_field.sendKeys(Keys.RETURN);
			Thread.sleep(3000);
			if ("Ready".equals(this.Status_of1stSearchedResult.getText()))
			{
				logger.info("Reporting '" + reportNames[i] + "' is run successfully!");
			}
			else {
				logger.error("Reporting '" + reportNames[i] + "' is run FAILED!!");
			}
			
		}
		
		
		
		this.Name_text_field.sendKeys();
		
	}
	
	
}
